package com.xiaobao.coalproperty.module.find.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.find.model.CarouselEntity;
import com.xiaobao.coalproperty.module.find.model.NewsListEntity;
import com.xiaobao.coalproperty.other.adapter.GlideImageLoader;
import com.xiaobao.coalproperty.other.adapter.NewsAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.youth.banner.Banner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

public class NewsActivity extends BaseActivity {


    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private View headView;
    private Banner mBanner;
    private NewsAdapter mNewsAdapter;

    private List<String> imageStrings;
    private int page = 1;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("新闻资讯");

        headView = LayoutInflater.from(this).inflate(R.layout.item_adapter_banner, null, false);
        mBanner = headView.findViewById(R.id.item_banner);

        mNewsAdapter = new NewsAdapter(this);
        mNewsAdapter.setOnItemClickListener(mOnItemClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mNewsAdapter);

        mBanner.setImageLoader(new GlideImageLoader());

        updateBanner();
        updateNewsList();
    }


    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            page=1;
            updateBanner();
            updateNewsList();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    private NewsAdapter.onItemClickListener mOnItemClickListener = new NewsAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(NewsActivity.this,NewsDetailActivivty.class);
            intent.setAction("url");
            intent.putExtra("url",mNewsAdapter.getAdapterData().get(position).getUrl());
            startActivity(intent);
        }
    };

    private void updateNewsList() {
        OkHttpUtils.post().url(UrlConstant.URL_NEWSLIST)
                .params(HttpHelperUtils.getInstance().newsListParams(page + ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    NewsListEntity entity = new Gson().fromJson(response, NewsListEntity.class);
                    if(entity.getCode()==0){
                        if(entity.getData().getList().size()>0){
                            if(page==1){
                                mNewsAdapter.setAdapterData(entity.getData().getList());
                            }else{
                                mNewsAdapter.addAdapterData(entity.getData().getList());
                            }
                            page++;
                        }else{
                            showToast("没有更多了");
                        }
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateBanner() {
        OkHttpUtils.post().url(UrlConstant.URL_CAROUSEL)
                .params(HttpHelperUtils.getInstance().carouselParams("3"))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    CarouselEntity entity = new Gson().fromJson(response, CarouselEntity.class);
                    if (entity.getCode() == 0) {
                        imageStrings = new ArrayList<>();
                        for (int i = 0; i < entity.getData().size(); i++) {
                            imageStrings.add(entity.getData().get(i).getPicture());
                        }
                        mBanner.setImages(imageStrings);
                        mBanner.start();
                    }
                } catch (Exception e) {
                }
            }
        });
    }
}
